//邮箱前端验证
$(document).ready(function () {
    // alert('8787');
    $('.register-form').submit(function () {
        var username = $('.register-username').val();
        var email = $('.register-email').val();
        var password1 = $('.register-password1').val();
        var password2 = $('.register-password2').val();
        var emailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        // console.log(username+email+password1+password2);
        if (!username || username.length > 10){
            $('.registerError').html('用户名不符合规范').show();
                    return false;
        }
        if (!email || !emailreg.test(email)){
            $('.registerError').html('邮箱不符合规范').show();
                    return false;
        }
        if (password1.length<6 || password2.length <6){
            $('.registerError').html("password not long").show();
            return false;
        }
        if (password1 !== password2){
            $('.registerError').html('两次密码不等').show();
            return false;
        }
        $.ajaxSetup({
            data:{csrfmiddlewaretoken: '{{ csrf_token }}'}
        });
        $.ajax({
            // async: false,
            type:"POST",
            url:"/auser/registerVerify/",
            data:{'username':username,'email':email,'password':password1},
            dataType:'json',
            success:function (arg) {
                // alert(arg);
                // setTimeout(function(){},2000);
                if (arg === 1){
                    // window.location.href = '/';
                    location.href = '/auser/login/';
                }
                if(data === -1 || data === 0 ){
                    $('.registerError').html('用户名或邮箱已存在').show();
                }
            },
            error:function (e) {
                $('.registerError').html('请求失败,请刷新页面后重试').show();
            }
        })
    });
    return false;
});